package com.acme.bankapp.domain.bank;

import java.io.Serializable;
import java.util.ArrayList;

import com.acme.bankapp.exceptions.bank.ClientExistsException;
import com.acme.bankapp.exceptions.bank.NotEnoughFundsException;

public class Client implements Serializable{
	private ArrayList<Account> accounts = new ArrayList<>();
	private String name;
	private Gender gender;
	

	
	public Client(String name, Gender gender,int ... initialValueAndOverDraft) throws IllegalArgumentException{
		this.name = name;
		this.gender = gender;
		
		if(initialValueAndOverDraft.length == 1){
			Account a = new SavingsAccount(initialValueAndOverDraft[0]);
			accounts.add(a);
		}
		else if(initialValueAndOverDraft.length == 2){
			Account a = new CheckingAccount(initialValueAndOverDraft[0],
					initialValueAndOverDraft[1]);
			accounts.add(a);
		}
		else{
			throw new IllegalArgumentException("One or two arguments are needed!");
		}

	}
	
	public String getName(){
		return name;
	}
	
	public String getGender(){
		return gender.getString();
	}
	
	public ArrayList<Account> getAccounts(){
		return accounts;
	}
	
	public void getClientSalutation(){
		System.out.print(gender.getString() + name);
	}
	
	public boolean equals(Object object){
		if(this == object){
			return true;
		}
		if(object == null){
			return false;
		}
		if(this.getClass() != object.getClass()){
			return false;
		}
		Client ob = (Client)object;
		if (this.getName() == ob.getName() && this.getGender() == ob.getGender()){
			return true;
		}
		else{
			return false;
		}
	}
	
	public int hashCode(){
		int code = 11;
		int k = 7;
		code = k*code + name.hashCode();
		code = k*code + gender.hashCode();
		return code;
	}
	
	public String toString(){
		StringBuffer sb = new StringBuffer("Client: name -- ");
		sb.append(name);
		sb.append(", gender -- ");
		sb.append(gender);
		sb.append("\nAccounts: ");
		for(Account ac : accounts){
			sb.append(ac.toString());
		}
		return sb.toString();
	}
}
